﻿using BW.EatRabbit.API.Application.Commands;
using BW.EatRabbit.API.Application.Commands.Breeding;
using BW.EatRabbit.API.Application.Commands.Cities;
using BW.EatRabbit.API.Application.Commands.DeeprocessingCompany;
using BW.EatRabbit.API.Application.Commands.Farming;
using BW.EatRabbit.API.Application.Commands.Immune;
using BW.EatRabbit.API.Application.Commands.Processing;
using BW.EatRabbit.API.Application.Commands.Provider;
using BW.EatRabbit.API.Application.Commands.Province;
using BW.EatRabbit.API.Application.Commands.Slaughter;
using BW.EatRabbit.API.Application.Commands.SlaughterCompany;
using BW.EatRabbit.API.Dtos;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace BW.EatRabbit.API.Controllers
{
    /// <summary>
    /// 控制器-API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public ManagementController(IMediator mediator, ILogger<ManagementController> logger)
        {

            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回登录的任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<APIResult<UserDTO>> Login(LoginQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增基本信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回新增基本信息的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> CreateBasicInformation(Basic_InformationCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <returns>返回获取商品信息的任务</returns>
        [HttpGet]
        public Task<APIResult<List<Goods>>> GetGoods()
        {
            return mediator.Send(new GoodsQueryCommand());
        }
        /// <summary>
        /// 获取基本信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回基本信息</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<BasicDTO>>> GetBasic([FromQuery] BasicInformationQueryCommand command)
        {
            return mediator.Send(command);

        }
        /// <summary>
        /// 逻辑删除基本信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回逻辑删除的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> DeleteBasic(BasicInformationDeleteCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 更新基本信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回更新基本信息</returns>
        [HttpPost]
        public Task<APIResult<int>> UpdateBasic(BasicInformationUpdateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回逻辑批量删除的任务</returns>
        [HttpDelete]
        public Task<APIResult<int>> LoicDelete(string idment)
        {
            BasicInformationAllDeleteCommand command = new();
            command.idment = idment;



            return mediator.Send(command);
        }
        /// <summary>
        /// 获取基本信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>返回获取基本信息的任务</returns>
        [HttpGet]
        public Task<APIResult<BasicDTO>> GetBasicSession(string productionBatch)
        {
            GetBasicInformationQueryCommad commad = new GetBasicInformationQueryCommad();
            commad.ProductionBatch = productionBatch;
            return mediator.Send(commad);
        }
        /// <summary>
        /// 新增养殖信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>新增养殖信息的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddBreeding(BreedingCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        ///  根据生产批次获取养殖信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>返回获取养殖信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Breeding_Information>> GetBreeding(string productionBatch)
        {
            GetBreedingQueryCommand command=new GetBreedingQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增保健信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回新增保健信息</returns>
        [HttpPost]
        public Task<APIResult<int>> ImmuneAdd(ImmuneCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增养殖企业信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回获取养殖信息的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddFarming(FarmingCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 根据生产批次获取企业信息
        /// </summary>
        /// <param name="productionBatch"></param>
        /// <returns>返回获取企业信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Farming_Company>> GetFarming(string productionBatch)
        {
            FarmingQueryCommand command = new FarmingQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增屠宰信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回新增屠宰的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddSlaughter(SlaughterCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取屠宰信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>获取屠宰信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Slaughter_Information>> GetSlaughter(string productionBatch)
        {
            SlaughterQueryCommand command = new SlaughterQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
  

        }
        /// <summary>
        /// 获取省的信息
        /// </summary>
        /// <returns>返回省的信息</returns>
        [HttpGet]
        public Task<APIResult<List<Province_Information>>> GetProvince()
        {
            return mediator.Send(new GetProvinceQueryCommand());
        }
        /// <summary>
        /// 根据省编号区查询市的信息
        /// </summary>
        /// <param name="provinceId">省级编号</param>
        /// <returns>根据省编号区查询市的信息的任务</returns>
        [HttpGet]
        public Task<APIResult<List<CityDTO>>> GetCity(int provinceId)
        {
            GetCitiesQueryCommand command=new GetCitiesQueryCommand();
            command.ProvinceId = provinceId;
            return mediator.Send(command);

        }
        /// <summary>
        /// 新增屠宰企业信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>新增屠宰企业信息的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddSlaugtherCompany(CompanyCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取屠宰公司的信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>获取屠宰公司的信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Slaughter_Company>> GetCompany(string productionBatch)
        {
            CompanyQueryCommand command = new CompanyQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);

        }
        /// <summary>
        /// 新增深加工信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回深加工的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddProcessing(ProcessingCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取深加工信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>获取深加工信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Processing_Information>> GetProcessing(string productionBatch)
        {
            GetProcessingQueryCommand command = new GetProcessingQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增深加工企业信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回深加工企业的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> AddDeepCompany(DeepCompanyCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取深加工信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>获取深加工信息的任务</returns>
        [HttpGet]
        public Task<APIResult<DeepProcessing_Company>> GetDeeprocessCompany(string productionBatch)
        {
            GetDeeprocessCompanyCommand command = new GetDeeprocessCompanyCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
        }
        /// <summary>
        /// 新增供应商信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回供应商的任务</returns>
        [HttpPost]
        public Task<APIResult<int>> Addprovider(ProviderCreateCommand command)
        {
            string UserNickname = User.FindFirstValue("UserNickname");
            command.UserNickname = UserNickname;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>获取供应商信息的任务</returns>
        [HttpGet]
        public Task<APIResult<Provider_Information>> Getprovider(string productionBatch)
        {
            GetProviderQueryCommand command = new GetProviderQueryCommand();
            command.ProductionBatch = productionBatch;
            return mediator.Send(command);
        }
    }
}
